package com.hkts.management.message.mq;

import com.hkts.management.common.mq.FanoutRabbitConfig;
import com.hkts.management.common.mq.IMessage;
import com.hkts.management.common.mq.MineType;
import com.hkts.management.common.utils.BeanConvertUtils;
import com.hkts.management.message.msg.service.serviceimpl.MsgTemplateServiceImpl;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

/**
 * @ClassNameFanoutReceiverA
 * @Description
 * @Author yck
 * @Date2021-04-26 11:10
 * @Version V1.0
 **/
@Component
public class FanoutReceiverA {
    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private MsgTemplateServiceImpl msgTemplateService;

/*    @RabbitListener(queues = FanoutRabbitConfig.Reminders_Queue_Name)
    public void process1(String content, Message message, Channel channel) throws IOException {
        try {
            logger.info("普通队列的内容[{}]", content);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            logger.error("处理失败:{}", e.getMessage());
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
        }
    }*/

    @RabbitHandler
    @RabbitListener(queues = FanoutRabbitConfig.Reminders_Queue_Name)
    public void process(Map testMessage, Message message, Channel channel)throws IOException {
        logger.info("reminders.queue消费者收到消息1  : " +testMessage.toString());
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        IMessage msg= BeanConvertUtils.mapToObject(testMessage,IMessage.class);
        if(MineType.TEMPLATE.getIndex().equals(msg.getMineType())){
            msgTemplateService.sendSmsMessage(msg);
        }else if(MineType.RESIDUAL.getIndex().equals(msg.getMineType())){
            msgTemplateService.residual(msg.getParam());
        }
    }

}
